<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 左侧的内容 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="showDialog = true"
                >新增角色</el-button
              >
            </el-row>
            <!-- 给表格绑定数据 -->
            <el-table border="" :data="list">
              <el-table-column
                align="center"
                type="index"
                width="120"
                label="序号"
              >
              </el-table-column>
              <el-table-column
                align="center"
                prop="name"
                width="240"
                label="名称"
              >
              </el-table-column>
              <el-table-column align="center" prop="description" label="描述">
              </el-table-column>
              <el-table-column align="center" label="操作">
                <!-- 作用域插槽 -->
                <template slot-scope="{ row }">
                  <el-button
                    @click="assignPerm(row.id)"
                    size="small"
                    type="success"
                    >分配权限</el-button
                  >
                  <el-button
                    @click="editRole(row.id)"
                    size="small"
                    type="primary"
                    >编辑</el-button
                  >
                  <el-button
                    size="small"
                    type="danger"
                    @click="deleteRole(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 放置分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :page-size="page.pagesize"
                :current-page="page.page"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              :closable="false"
              :show-icon="true"
            >
            </el-alert>
            <!-- 右侧的内容 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="企业名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="电话">
                <el-input
                  v-model="formData.companyPhone"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 放置弹层 -->
     
    </div>
    <!-- 放置一个弹层组件 -->
    <!-- close事件在点击确定时会触发 -->
    <el-dialog @close="btnCancel" title="编辑部门" :visible="showDialog">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 放置footer插槽 -->
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button @click="btnCancel" size="small" type="">取消</el-button>
          <el-button @click="btnOk" size="small" type="primary">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 放置弹层 -->
    <el-dialog
      @close="btnPermCancel"
      title="分配权限"
      :visible="showPermDialog"
    >
      <!-- 权限是一棵树  -->
      <!-- 将数据绑定到组件 -->
      <el-tree
        ref="permTree"
        :default-expand-all="true"
        :data="permData"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
        :default-checked-keys="selectCheck"
      />
      <!-- 确定 取消 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button @click="btnPermOk" size="small" type="primary"
            >确定</el-button
          >
          <el-button @click="btnPermCancel" size="small">取消</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  getRoleDetail,
  updateRole,
  addRole,
  assignPerm,
} from "@/api/setting";
import { getPermissionList } from "@/api/permission";
import { mapGetters } from "vuex";
import { Message } from "element-ui";
import { tranListToTreeData } from "@/utils";
export default {
  data() {
    return {
      list: [], //承接数组
      page: {
        // 放置页码及相关数据
        page: 1,
        pagesize: 10,
        total: 0, //记录总数
      },
      permData: [], //接收权限数据
      defaultProps: {
        label: "name",
      }, //定义显示字段的名称和子属性的字段名称
      formData: {
        //公司信息
      },
      showDialog: false, //控制弹层显示
      showPermDialog: false, // 控制分配权限弹层
      roleForm: {
        name: "",
        description: "",
      },
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
      },
      roleId: null, //用来记录当前分配权限的id
      selectCheck: [], // 用来记录当前权限点的标识
    };
  },
  computed: {
    ...mapGetters(["companyId"]),
  },
  created() {
    this.getRoleList(); //获取角色列表
    this.getCompanyInfo();
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page);
      this.page.total = total;
      this.list = rows;
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId);
      // 直接赋值给FormData
    },
    changePage(newPage) {
      // newPage是当前点击的页码
      this.page.page = newPage; //将当前页码赋值给当前的最新页码
      this.getRoleList();
    },
    // 删除
    async deleteRole(id) {
      // 提示
      try {
        await this.$confirm("确认删除该角色吗？");
        // 只有点击了确定才能执行到下方
        await deleteRole(id); //调用删除接口
        this.getRoleList(); // 重新加载数据
        this.$message.success("删除成功");
      } catch (error) {}
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id); //实现数据的回写
      this.showDialog = true; //显示弹层
    },
    async btnOk() {
      try {
        await this.$refs.roleForm.validate();
        // 只有校验通过的情况下，才会执行await的下方内容
        // roleForm这个对象有id就是编辑，没有id就是新增
        if (this.roleForm.id) {
          await updateRole(this.roleForm);
        } else {
          // 新增业务
          await addRole(this.roleForm);
        }
        // 重新获取数据
        this.getRoleList();
        this.$message.success("操作成功");
        this.showDialog = false; // 关闭弹层 => 触发el-dialog的事件close事件
      } catch (error) {}
    },
    btnCancel() {
      this.roleForm = {
        name: "",
        description: "",
      };
      // 移除校验
      this.$refs.roleForm.resetFields();
      this.showDialog = false;
    },
    // 弹出层
    // 获取权限点数据 在点击的时候调用获取权限点数据
    async assignPerm(id) {
      this.permData = tranListToTreeData(await getPermissionList(), "0"); //获取所有权限点 ， 转化之后的list数据
      // 有id了就可以 id应该先记录下来
      this.roleId = id;
      // 去获取这个id的权限点
      const { permIds } = await getRoleDetail(id); //permIds是当前角色所拥有的权限点数据
      this.selectCheck = permIds; // 将当前角色拥有的权限id赋值
      this.showPermDialog = true;
    },
    async btnPermOk() {
      // 确定 调用el-tree方法
      // this.$refs.permTree.getCheckedKeys();
      await assignPerm({
        permIds: this.$refs.permTree.getCheckedKeys(),
        id: this.roleId,
      });
      this.$message.success("分配权限成功");
      this.showPermDialog = false;
    },
    btnPermCancel() {
      // 取消
      this.selectCheck = []; //重置数据
      this.showPermDialog = false;
    },
  },
};
</script>

<style>
</style>